package com.wengzw.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wengzw.po.Customer;
import com.wengzw.po.User;
import com.wengzw.service.UserService;
import com.wengzw.utils.DataGridViewResult;
import com.wengzw.vo.CustomerVo;
import com.wengzw.vo.UserVo;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    //创建map集合保存信息
    Map<String,Object> map = new HashMap<String, Object>();
    @RequestMapping("/login")
    //接收到数据，调用service
    public String login(String user_name, String user_password, HttpSession session){
        // System.out.print("23333");
        //调用登陆方法
        User loginUser = userService.login(user_name,user_password);
        if(loginUser!=null){
            session.setAttribute("loginUser",loginUser);
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        //将map集合的数据以json格式返回
        return JSON.toJSONString(map);
    }
    
    @RequestMapping("/logout")
    public void logout(HttpSession session,HttpServletResponse response) throws IOException {
    	if(session.getAttribute("loginUser") != null) {
    		session.removeAttribute("loginUser");
    		map.put("success",true);
    		response.sendRedirect("/login");
    	}else {
    		map.put("success",false);
    		response.sendRedirect("/login");
    	}
    }
    @RequestMapping("/userList")
    public DataGridViewResult clientList(UserVo userVo) {
        //Map<String, Object> map = new HashMap<String, Object>();
        //设置分页信息(当前页码，每页显示数量）
        PageHelper.startPage(userVo.getPage(),userVo.getLimit());
//        System.out.print(customerVo.getPage());
//        System.out.print(customerVo.getLimit());
        //调用分页查询账单列表的方法

        List<User> userList = userService.findUserList(userVo);
        //创建分页对象
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        //将数据表格返回的key保存到map集合中
//        map.put("code",0);
//        map.put("msg","");
//        map.put("count",pageInfo.getTotal());//总数量
//        map.put("data",pageInfo.getList()); //获取列表
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    //添加客户
    @RequestMapping("/adduser")
    public String addUser(User user){
        Map<String,Object> map = new HashMap<String, Object>();
        if(userService.addUser(user)>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else{
            map.put("success",false);
            map.put("message","添加失败");
        }
        return JSON.toJSONString(map);
    }

    //修改客户
    @RequestMapping("/updateuser")
    public String updateUser(User user){
        Map<String,Object> map = new HashMap<String, Object>();
        if(userService.updateUser(user)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else{
            map.put("success",false);
            map.put("message","修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @RequestMapping("/deleteUserById")
    public String deleteUserById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(userService.deleteById(id)>0){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @RequestMapping("/batchUserDelete")
    public String batchUserDelete(String ids){
        System.out.println(ids);
        Map<String,Object> map = new HashMap<String,Object>();
        int count = 0;
        //将字符串拆分成数组
        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            count = userService.deleteById(Integer.valueOf(idsStr[i]));
            if(count>0){
                map.put("success",true);
                map.put("message","删除成功");
            }
        }
        //判断受影响行数是否为0
        if(count<=0){
            map.put("success",false);
            map.put("message","删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/findUserById")
    public String findUserById(Integer id){
        System.out.println(id);
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(userService.findUserByID(id));
        if(userService.findUserByID(id)!=null){
            map.put("success",true);
            map.put("message","查找成功");
            map.put("info",userService.findUserByID(id));
        }else{
            map.put("success",false);
            map.put("message","查找失败");
        }
        return JSON.toJSONString(map);
    }
}
